import 'points_package.dart';

/// 积分套餐列表响应模型
class PointsPackageResponse {
  final String status;
  final String message;
  final List<PointsPackage> packages;

  PointsPackageResponse({
    required this.status,
    required this.message,
    required this.packages,
  });

  /// 从JSON创建PointsPackageResponse对象
  factory PointsPackageResponse.fromJson(Map<String, dynamic> json) {
    List<PointsPackage> packageList = [];
    
    if (json['data'] != null && json['data'] is List) {
      packageList = (json['data'] as List)
          .map((item) => PointsPackage.fromJson(item))
          .toList();
    }

    return PointsPackageResponse(
      status: json['status'] ?? 'error',
      message: json['message'] ?? '',
      packages: packageList,
    );
  }

  /// 转换为JSON
  Map<String, dynamic> toJson() {
    return {
      'status': status,
      'message': message,
      'data': packages.map((package) => package.toJson()).toList(),
    };
  }

  /// 是否成功
  bool get isSuccess => status == 'success';

  /// 错误信息
  String get errorMessage => isSuccess ? '' : message;

  /// 套餐数量
  int get packageCount => packages.length;

  /// 获取热门套餐
  List<PointsPackage> get popularPackages => 
      packages.where((package) => package.isPopular).toList();

  /// 获取激活的套餐
  List<PointsPackage> get activePackages => 
      packages.where((package) => package.isActive).toList();

  /// 按排序顺序获取套餐
  List<PointsPackage> get sortedPackages {
    List<PointsPackage> sorted = List.from(packages);
    sorted.sort((a, b) => a.sortOrder.compareTo(b.sortOrder));
    return sorted;
  }

  @override
  String toString() {
    return 'PointsPackageResponse{status: $status, message: $message, packageCount: $packageCount}';
  }
}
